package com.kaishengit.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.dao.ProdDao;
import com.kaishengit.entity.Prod;

public class ProdServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String task = request.getParameter("task");
		String method = request.getMethod();
	
		if("".equals(task)||task==null){
			list(request,response);
		}else if("add".equals(task)){
			add(request,response);
		}else if("del".equals(task)){
			del(request,response);
		}else if("edit".equals(task)){
			if("GET".equals(method)){
				edit(request,response);
			}else if("POST".equals(method)){
				toEdit(request,response);
			}
		}
		
	}
	
	private void toEdit(HttpServletRequest request, HttpServletResponse response) {
		String num = request.getParameter("num");
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String enable = request.getParameter("enable");
		
		ProdDao dao = new ProdDao();
		Prod prod = dao.findByNum(num);
		
		prod.setProd_num(num);
		prod.setProd_name(name);
		prod.setProd_price(price);
		prod.setEnable(Boolean.parseBoolean(enable));
		
		dao.edit(prod);
	
		
	}

	private void edit(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		String num = request.getParameter("num");
		ProdDao dao = new ProdDao();
		Prod prod = dao.findByNum(num);
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		out.print("<prod>");
		out.print("<prod_num>"+prod.getProd_num()+"</prod_num>");
		out.print("<prod_name>"+prod.getProd_name()+"</prod_name>");
		out.print("<prod_price>"+prod.getProd_price()+"</prod_price>");
		out.print("<enable>"+prod.isEnable()+"</enable>");
		out.print("</prod>");
		out.flush();
		out.close();
		
		
	}

	private void del(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		String num = request.getParameter("num");
		ProdDao dao = new ProdDao();
		dao.del(num);
		response.sendRedirect("prod.jsp");
	}
	
	
	private void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		String num = request.getParameter("num");	
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String enable = request.getParameter("enable");
		
		Prod p = new Prod();
		p.setProd_num(num);
		p.setProd_name(name);
		p.setProd_price(price);
		p.setEnable(Boolean.parseBoolean(enable));
		
		ProdDao dao = new ProdDao();
		dao.save(p);
		response.sendRedirect("prod.jsp");
		
		
	}
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProdDao dao = new ProdDao();
		List<Prod> list = dao.findAll();
		request.setAttribute("list", list);
		
		request.getRequestDispatcher("prod.jsp").forward(request, response);
		
		
	}

}
